import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../../l10n/local.dart';
import '../../../routes/app_routes.dart';
import 'controller.dart';

class BleFAQView extends BaseGetView<BleFAQController> {
  BleFAQView({super.key});

  @override
  String get title => local.common_issues;

  @override
  Widget buildBody(BuildContext context) {
    return ListView(
      padding: const EdgeInsets.all(16),
      children: [
        _buildHeader(),
        const SizedBox(height: 24),
        ..._buildFAQItems(),
        const SizedBox(height: 24),
        _buildContactSection(),
      ],
    );
  }

  Widget _buildHeader() {
    return Column(
      children: [
        Icon(
          Icons.bluetooth_disabled,
          size: 64,
          color: Colors.grey[400],
        ),
        const SizedBox(height: 16),
        Text(
          local.bluetooth_connection_issues,
          style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
            color: Colors.grey[800],
          ),
        ),
        const SizedBox(height: 8),
        Text(
          local
              .please_check_the_following_FAQs_to_quickly_resolve_your_connection_issues,
          style: TextStyle(
            fontSize: 16,
            color: Colors.grey[600],
          ),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }

  List<Widget> _buildFAQItems() {
    final faqItems = [
      {
        'question': local.question_trouble_discovering_bluetooth_devices,
        'answer': local.answer_trouble_discovering_bluetooth_devices
      },
      {
        'question': local.question_frequent_connection_drops,
        'answer': local.answer_frequent_connection_drops
      },
      {
        'question': local.question_why_does_pairing_fail,
        'answer': local.answer_why_does_pairing_fail
      },
      {
        'question': local.question_how_to_reset_bluetooth_connection,
        'answer': local.answer_how_to_reset_bluetooth_connection
      },
      {
        'question': local.question_required_app_permissions,
        'answer': local.answer_required_app_permissions
      },
    ];

    return faqItems.map((item) => _buildFAQItem(item)).toList();
  }

  Widget _buildFAQItem(Map<String, String> item) {
    return Card(
      margin: const EdgeInsets.only(bottom: 16),
      elevation: 2,
      child: ExpansionTile(
        leading: const Icon(Icons.bluetooth, color: Colors.blue),
        title: Text(
          item['question']!,
          style: const TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 16,
          ),
        ),
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Text(
              item['answer']!,
              style: const TextStyle(fontSize: 14, height: 1.5),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildContactSection() {
    return Card(
      color: Colors.blue[50],
      child: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            const Icon(Icons.support_agent, size: 48, color: Colors.blue),
            const SizedBox(height: 16),
            Text(
              local.problem_still_persists,
              style: const TextStyle(
                color: Colors.black,
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            Text(
              local.please_contact_our_technical_support_department,
              style: const TextStyle(color: Colors.black, fontSize: 14),
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                _buildContactButton(
                  icon: Icons.chat,
                  label: local.online_support_service,
                  onPressed: () {
                    // 这里可以跳转到客服聊天页面
                    Get.toNamed(AppRoutes.CONTACT_ONLINE_SERVICE);
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildContactButton({
    required IconData icon,
    required String label,
    required VoidCallback onPressed,
  }) {
    return Column(
      children: [
        IconButton(
          icon: Icon(icon, size: 32),
          onPressed: onPressed,
          color: Colors.blue,
        ),
        Text(label, style: const TextStyle(color: Colors.black, fontSize: 12)),
      ],
    );
  }
}
